package com.zzsy.carosshopprojects.activitys.home.mainpage.barmanager.membershipcard;

import android.content.Context;

import com.king.base.util.ToastUtils;
import com.zzsy.carosshopprojects.base.BaseViewMode;
import com.zzsy.carosshopprojects.base.ConfigureFile;
import com.zzsy.carosshopprojects.bean.BaseBean;
import com.zzsy.carosshopprojects.bean.MainCardListBean;
import com.zzsy.carosshopprojects.network.RequestUtils;
import com.zzsy.carosshopprojects.network.Utils.MyObserver;
import com.zzsy.carosshopprojects.util.DataUtil;

import androidx.lifecycle.MutableLiveData;

public class CardReplaceMode extends BaseViewMode {
    private MutableLiveData<String> liveData;
    private MutableLiveData<MainCardListBean> liveDataList;

    public MutableLiveData<String> getLiveData() {
        if (liveData == null)
            liveData = new MutableLiveData<>();
        return liveData;
    }

    public MutableLiveData<MainCardListBean> getliveDataList() {
        if (liveDataList == null)
            liveDataList = new MutableLiveData<>();
        return liveDataList;
    }

    /**
     * 获取验证码
     *
     * @param context
     * @param phone
     */
    public void getCode(Context context, String phone) {
        RequestUtils.GetCode(context, phone, new MyObserver<BaseBean>(context) {
            @Override
            public void onSuccess(BaseBean result) {

            }

            @Override
            public void onFailure(Throwable e, String errorMsg, int code) {
                if (code == ConfigureFile.code) {
                    liveData.postValue(errorMsg);
                } else {
                    ToastUtils.showToast(context.getApplicationContext(), errorMsg);
                }
            }
        });
    }

    /**
     * 验证手机号以及验证妈
     *
     * @param context
     * @param phone
     * @param msgId
     * @param code
     */
    public void CheckCode(Context context, String phone, String msgId, String code) {
        RequestUtils.CheckCode(context, phone, msgId, code, new MyObserver<BaseBean>(context,false) {
            @Override
            public void onSuccess(BaseBean result) {

            }

            @Override
            public void onFailure(Throwable e, String errorMsg, int code) {
                if (code == ConfigureFile.code) {
                    getMyMainCardsByPhone(context, phone);
                } else {
                    ToastUtils.showToast(context.getApplicationContext(), errorMsg);
                }
            }
        });
    }
    /**
     * 验证改手机号是否是会员
     */
    private void getMyMainCardsByPhone(Context context, String phone) {
        RequestUtils.MyMainCardsByPhone(context, DataUtil.getInstance().getCompanyId(), phone, new MyObserver<MainCardListBean>(context) {
            @Override
            public void onSuccess(MainCardListBean result) {
                liveDataList.postValue(result);
            }

            @Override
            public void onFailure(Throwable e, String errorMsg, int code) {
                ToastUtils.showToast(context.getApplicationContext(), errorMsg);
            }
        });
    }


}
